package org.rcgwt.client.ui.grid;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Simple Widget with button for pagination feature
 * 
 * @author Matthieu ROBERT
 * 
 */
public class PageButtonsPanel extends Composite {

    private final RcGrid grid;

    private Label currentPageText;

    private Button nextPageButton, previousPageButton;

    private TextBox pageSizeTextBox;

    public PageButtonsPanel(RcGrid grid) {

        this.grid = grid;

        HorizontalPanel p = new HorizontalPanel();

        // Previous
        previousPageButton = new Button("Previous", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                previousPage();

            }
        });
        p.add(previousPageButton);

        // Counter
        currentPageText = new Label();
        p.add(currentPageText);

        nextPageButton = new Button("Next", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                nextPage();

            }
        });
        p.add(nextPageButton);

        // page size
        p.add(new Label("Page Size"));
        pageSizeTextBox = new TextBox();
        pageSizeTextBox.addKeyDownHandler(new KeyDownHandler() {

            @Override
            public void onKeyDown(KeyDownEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                    performPageSize();
                }

            }
        });
        pageSizeTextBox.setText(Integer.toString(grid.getPageSize()));
        p.add(pageSizeTextBox);

        p.add(new Button("OK", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                performPageSize();
            }
        }));

        updatePagePanel();
        initWidget(p);
    }

    private void performPageSize() {
        try {
            int pageSize = Integer.parseInt(pageSizeTextBox.getText());
            grid.setPageSize(pageSize);
            grid.gotoRowIndex(0);
        } catch (NumberFormatException ex) {

        }

    }

    private void nextPage() {
       if( grid.nextPage())
    	   updatePagePanel();
    }

    private void previousPage() {
       if( grid.previousPage())
    	   updatePagePanel();
    }

    public void updatePagePanel() {
        /*
        int currentRowIndex = grid.getCurrentRowIndex();
        nextPageButton.setEnabled((currentRowIndex + 1 < grid.getPageCount()));
        previousPageButton.setEnabled((currentRowIndex > 0));
        currentPageText.setText((grid.getCurrentPage() + 1) + " / "
                + (grid.getPageCount()));

        pageSizeTextBox.setText(Integer.toString(grid.getPageSize()));
        */
    }

}
